package com.gm.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.gm.springboot.bean.Manga;
import com.gm.springboot.service.MangaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 阮渝轩
 * @Date: 2020/7/4 11:00
 * @Version 1.0
 */
@Controller
public class MangaController {
    @Autowired
    MangaService mangaService;
    @GetMapping("/mangas")
    public String findStudentAll(@RequestParam(defaultValue = "1")Integer page,
                                 @RequestParam(defaultValue = "5")Integer size,
                                 Model model
    ){
        List<Manga> list=this.mangaService.findMangaAll(page,size);
        PageInfo<Manga> pageInfo=new PageInfo<>(list);
        model.addAttribute("mangas",pageInfo);
        System.out.println("114");
        return "manga-list";

    }
    @GetMapping("/delManga/{mid}")
    public String delManga(@PathVariable("mid")Integer mid){
        this.mangaService.delManga(mid);
        return "redirect:/mangas";
    }
    @PostMapping("/addManga")
    public String addManga(Manga manga){
        this.mangaService.addManga(manga);
        return "redirect:/mangas";
    }
    @GetMapping("/toMangaPage/{mid}")
    public String toMangaPage(@PathVariable("mid")Integer mid,Model model){
        Manga manga=this.mangaService.findMangaById(mid);
        model.addAttribute("manga",manga);
        return "manga-upd";
    }

    @PutMapping("/updManga")
    public String updManga(Manga manga){
        this.mangaService.updManga(manga);
        return "redirect:mangas";
    }
}
